﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FurnitureRentalSystem
{
    public partial class LoginDlg : Form
    {
        private MySql.Data.MySqlClient.MySqlConnection conn;
        public LoginDlg(String connString)
        {
            conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Opens a new connection and attempts to validate employee login
                conn.Open();
                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();
                cmd.CommandText = "SELECT * FROM GroupWHL.employee WHERE eID = " + eIDtxt.Text + " AND Password = " + passwordTxt.Text;
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    DialogResult = DialogResult.OK;
                    conn.Close();
                }
                else
                {
                    MessageBox.Show("Invalid Login", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            conn.Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
